package com.yyfax.ph.personal.authority.login;

import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.Model;
import com.yyfax.infrastructure.network.NetworkHelper;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yyfax.ph.base.YJPH;
import retrofit2.http.Body;
import retrofit2.http.Header;
import retrofit2.http.POST;
import rx.Observable;

public class LoginModel extends Model {
	private static final String TAG = LoginModel.class.getSimpleName();

	@Override
	public void onNext(Object obj) {
		CLog.i(TAG, "onNext: " + obj.toString());
		LoginResult login = cast(obj);
		EventAgent.post(login);
	}

	@Override
	public Observable getObservable(Object params, int functionalCode) {
		Service service = (Service) NetworkHelper.createService(Service.class);
		String token = YJPH.getInstance().getPreferenceManager().getLoginToken();
		return service.login(token, (Login) params);
	}

	public interface Service {
		@POST("sys/user/login")
		Observable<LoginResult> login(@Header("token") String token, @Body Login params);
	}

	public static class Login {
		public Login(String loginName, String password) {
			this.loginName = loginName;
			this.password = password;
		}

		String loginName;
		String password;
	}

	public static class LoginResult extends BaseResultEntity<LoginContentBean> {
	}

	public static class LoginContentBean {
		/***
		 * 用户推荐码
		 */
		public String id;
		/***
		 * 登录标识 0：正常、1：禁止登录、2：审批、3：首次登录
		 */
		public int delFlag;
		/***
		 * 用户名（若不返回，则未实名）
		 */
		public String name;
		/***
		 *用户类型 1：个人中介、2：企业中介
		 */
		public int userType;
		/***
		 * 手机号
		 */
		public String phone;
		/***
		 * 推荐标识 0：禁止推荐、1：允许推荐
		 */
		public int rcmdFlag;
		/***
		 * 登录token
		 */
		public String token;

	}
}
